package main

import (
	. "whoops/leetcode/common"
)

//#link: https://leetcode-cn.com/problems/remove-linked-list-elements/
func main() {

}

//新增一个链表 [动谁，谁添加一个新的]
func removeElementsWay1(head *ListNode, val int) *ListNode {
	if head == nil {
		return head
	}
	resultNode := new(ListNode)
	curr := resultNode
	for head != nil {
		if head.Val != val {
			curr.Next = &ListNode{Val: head.Val}
			curr = curr.Next
		}
		head = head.Next
	}
	return resultNode.Next
}

//原有基础上做修改
func removeElementsWay2(head *ListNode, val int) *ListNode {
	if head == nil {
		return head
	}
	result := &ListNode{Next: head}
	for temp := result; temp.Next != nil; {
		if temp.Next.Val == val {
			temp.Next = temp.Next.Next
		} else {
			temp = temp.Next
		}
	}
	return result.Next
}

//迭代的方法实现
func removeElements(head *ListNode, val int) *ListNode {
	if head == nil {
		return head
	}
	head.Next = removeElements(head.Next, val)
	if head.Val == val {
		return head.Next
	}
	return head
}
